-- Author      : Kurapica

--------------------------
-- WidgetButton        --
--------------------------
do
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
    local _WidgetName = "WidgetButton"
    local _Base = IGAS.GUI.Widget.Button

    local _Widget = IGAS.Studio.Widget
    local _PropertyType = IGAS.GUI.Base.PropertyType

    -- Script 
    local function OnMouseDown(self)
        if self.ButtonState == "PUSHED" then
            self.ButtonState = "NORMAL"
			if self.Parent.Parent.__SelBtn == self then
				self.Parent.Parent.__SelBtn = nil
				self.Parent.Parent.Parent.Parent.Parent:Fire("OnWidgetSelected")
			end
        else
			if self.Parent.Parent.__SelBtn and self.Parent.Parent.__SelBtn ~= self then
				self.Parent.Parent.__SelBtn.ButtonState = "NORMAL"
			end
            self.ButtonState = "PUSHED"
			self.Parent.Parent.__SelBtn = self
			self.Parent.Parent.Parent.Parent.Parent:Fire("OnWidgetSelected")
       end
    end
	
    -- ScriptType
    local _ScriptType = {
    }

    --    FuncProxy
    local _FuncProxy = {
    }

    --    Property
    local _Property = {			
		-- ToggleState
		["Widget"] = {
			["Set"] = function(self, widget)
				self.__Widget = widget
				if widget and widget.Name then
					self.Text = widget.Name
				else
					self.Text = ""
				end
			end,

			["Get"] = function(self)
				return self.__Widget
			end,

			["Type"] = _PropertyType.Object,
		},
    }

    local function _New(parent)        
        local btn = CreateFrame("Button", nil, parent)

        btn.Style = "NORMAL"
        btn.Height = 20
        btn.Width = 140
        btn.Locked = true

        -- Parent's Settings
        local index = parent.__WidgetButtonNum or 0

		index = index + 1
		parent.__WidgetButtonNum = index
		btn.ID = index
		
        btn:StackScript("OnMouseDown", OnMouseDown, true)
		
		return btn
    end

    -- Register
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }

    _Widget[_WidgetName] = IGAS:NewWidget(_WidgetInfo)
end
